--[[
    @desc 界面item基类，定义了挂接父节点的逻辑和基本的显示隐藏，设置位置大小等逻辑
    @author zch
    @data:2022-04-18 15:14:00
]]

BaseItem = BaseItem or BaseClass()
local BaseItem = BaseItem
local LuaViewManager = LuaViewManager

function BaseItem:__init(parent)
	self.parent = parent
    self.base_file = ""
    self.layout_file = ""
    self.layer_name = "UI"
    self.transform = nil
    self.gameObject = nil
    self.anchoredPosition = nil
    self.is_loaded = false
end

function BaseItem:__delete()
	self:Destroy()
end

function BaseItem:Destroy()
	if not self.gameObject then
		return
	end
	LuaViewManager:GetInstance():DestroyItem(self.gameObject)
end

function BaseItem:Load()
	if not self.parent then
		print ("error parent_wnd is Null")
		return
	end

	if self.gameObject then
		return
	end

	if self.base_file ~= "" and self.layout_file ~= "" then
		local res_load_finish = function(obj)
			self:doCreateItemIndeed(obj)
		end
		self.is_loaded = false
		LuaViewManager:GetInstance():LoadItem(self.base_file, self.layout_file, self.parent, res_load_finish)
	end
end

function BaseItem:doCreateItemIndeed(obj)
	self.is_loaded = true
	self.gameObject = obj
	self.transform = obj.transform

	if self.LoadSuccess then
		self.LoadSuccess(self)
	end
	if self.anchoredPosition then
		self:SetAnchoredPosition(self.anchoredPosition.x, self.anchoredPosition.y)
	end
end

function BaseItem:SetAnchoredPosition(x, y)
	if self.is_loaded then
		setAnchoredPosition(self.transform, x, y)
	else
		self.anchoredPosition = {x = x, y = y}
	end
end